﻿namespace Yom.Common
{
    using System;
    using System.Configuration;
    using System.Text.RegularExpressions;
    using System.IO;
    using System.Web;
    using System.Xml;

    /// <summary>
    /// 公共方法操作类
    /// 
    /// 创建: 邱柯皓 2009/02/04 19:16
    /// </summary>
    public static class Factory
    {
        #region 基本方法
        /// <summary>
        /// 获取过滤后的POST, GET数据
        /// 
        /// 创建: 邱柯皓 2009/02/04 19:23
        /// </summary>
        /// <param name="index">数据索引</param>
        /// <returns>返回过滤后的数据</returns>
        public static string Request(string index)
        {
            if (HttpContext.Current.Request.Form[index] != null && HttpContext.Current.Request.Form[index] != "")
                return HttpContext.Current.Request.Form[index].SQLFilter().HtmlEncode().TrimEnd(null);
            else if (HttpContext.Current.Request.QueryString[index] != null && HttpContext.Current.Request.QueryString[index] != "")
                return HttpContext.Current.Request.QueryString[index].UrlEncode().SQLFilter().HtmlEncode().TrimEnd(null);
            else
                return "";
        }

        /// <summary>
        /// 获取指定路径的物理路径
        /// 
        /// 创建: 邱柯皓 2009/02/04 20:00
        /// </summary>
        /// <param name="Path">路径</param>
        /// <returns>返回物理路径</returns>
        public static string MapPath(string Path)
        {
            Path = Path.Replace(@"\\", @"\").Replace("//", "/");

            return HttpContext.Current.Server.MapPath(Path);
        }

        /// <summary>
        /// 获取模板列表
        /// 
        /// 创建: 邱柯皓 2009/03/06 23:41
        /// </summary>
        /// <returns>返回模板列表</returns>
        public static string[] ThemeList()
        {
            DirectoryInfo dir = new DirectoryInfo(RootPath + "\\Themes");
            DirectoryInfo[] ds = dir.GetDirectories();
            string[] dirList = new string[ds.Length];

            for (int i = 0; i < ds.Length; i++)
            {
                dirList.SetValue(ds[i].Name, 0);
            }

            return dirList;
        }

        /// <summary>
        /// 获取语言列表
        /// 
        /// 创建: 邱柯皓 2009/03/06 23:42
        /// </summary>
        /// <returns>返回语言列表</returns>
        public static string[] LocaleList()
        {
            DirectoryInfo dir = new DirectoryInfo(RootPath + "\\Locale");
            FileInfo[] fs = dir.GetFiles("*.xml");
            string[] fileList = new string[fs.Length];

            for (int i = 0; i < fs.Length; i++)
            {
                fileList[i] = fs[i].Name;
            }

            return fileList;
        }

        /// <summary>
        /// 获取分页链接
        /// 
        /// 创建: 邱柯皓 2009/03/07 15:58
        /// </summary>
        /// <param name="pageSize">每页显示数量</param>
        /// <param name="currPage">当前页索引</param>
        /// <param name="dataCount">数据总数</param>
        /// <param name="strP">页面链接替换字符串</param>
        /// <returns>返回分页链接</returns>
        public static string GetPager(int pageSize, int currPage, int dataCount, string strP)
        {
            System.Text.StringBuilder strPage = new System.Text.StringBuilder();
            int offest = 0;
            int page = 0;

            if (dataCount > pageSize)
            {
                page = 10;
                offest = 2;
                int pages = Math.Ceiling(Convert.ToDouble(dataCount) / Convert.ToDouble(pageSize)).ToInt();
                int from = currPage - offest;
                int to = currPage + page - offest - 1;

                if (page > pages)
                {
                    from = 1;
                    to = pages;
                }
                else
                {
                    if (from < 1)
                    {
                        to = currPage + 1 - from;
                        from = 1;

                        if ((to - from) < page && (to - from) < pages)
                            to = page;
                    }
                    else if (to > pages)
                    {
                        from = currPage - pages + to;
                        to = pages;

                        if ((to - from) < page && (to - from) < pages)
                            from = pages - page + 1;
                    }
                }
                strPage = new System.Text.StringBuilder("<a href=\"" + strP.Replace("<{Page}>", "1") + "\">&laquo;</a>");
                if (currPage != 1)
                    strPage.Append("<a href=\"" + strP.Replace("<{Page}>", (currPage - 1).ToString()) + "\">&#8249;</a>");
                for (int i = from; i <= to; i++)
                {
                    if (i != currPage)
                        strPage.Append("<a href=\"" + strP.Replace("<{Page}>", i.ToString()) + "\">" + i.ToString() + "</a>");
                    else
                        strPage.Append("<strong>" + i.ToString() + "</strong>");
                }
                if (currPage != pages)
                    strPage.Append("<a href=\"" + strP.Replace("<{Page}>", (currPage + 1).ToString()) + "\">&#8250;</a>");
                strPage.Append("<a href=\"" + strP.Replace("<{Page}>", pages.ToString()) + "\">&raquo;</a>");
            }

            return strPage.ToString();
        }

        /// <summary>
        /// 过滤文件后缀
        /// 
        /// 创建: 邱柯皓 2009/03/08 00:44
        /// </summary>
        /// <param name="strVal">文件后缀</param>
        /// <returns>返回过滤后的文件后缀</returns>
        public static string FilterFile(string strVal)
        {
            if (!string.IsNullOrEmpty(strVal))
            {
                strVal = strVal.Replace("/", "");
                strVal = strVal.Replace(@"\", "");
                strVal = strVal.Replace(":", "");
                strVal = strVal.Replace("*", "");
                strVal = strVal.Replace("?", "");
                strVal = strVal.Replace("\"", "");
                strVal = strVal.Replace("'", "");
                strVal = strVal.Replace("<", "");
                strVal = strVal.Replace(">", "");
                strVal = strVal.Replace("|", "");
                strVal = strVal.Replace(".", "");
                strVal = strVal.Replace(" ", "");
            }
            return strVal;
        }

        /// <summary>
        /// 检查是否为允许的文件类型
        /// 
        /// 创建: 邱柯皓 2009/03/08 00:46
        /// </summary>
        /// <param name="strExt">文件类型</param>
        /// <param name="strAll">允许的类型</param>
        /// <returns>返回是否允许</returns>
        public static bool CheckFile(string strExt, string strAll)
        {
            if (!string.IsNullOrEmpty(strExt) && !string.IsNullOrEmpty(strAll))
            {
                foreach (string str in strAll.Split(new char[] { ',' }))
                {
                    if (str.ToLower() == strExt.ToLower())
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// 得到一个汉字的拼音第一个字母，如果是一个英文字母则直接返回大写字母
        /// 
        /// 创建: 邱柯皓 2009/03/09 14:40
        /// </summary>
        /// <param name="str">要获取的汉字或英文</param>
        /// <returns>返回字母</returns>
        public static string GetCharSpellCode(string str)
        {
            if (str.CompareTo("吖") < 0)
            {
                string s = str.Substring(0, 1).ToUpper();
                if (char.IsNumber(s, 0))
                {
                    return "0";
                }
                else
                {
                    return s;
                }

            }
            else if (str.CompareTo("八") < 0)
            {
                return "A";
            }
            else if (str.CompareTo("嚓") < 0)
            {
                return "B";
            }
            else if (str.CompareTo("咑") < 0)
            {
                return "C";
            }
            else if (str.CompareTo("妸") < 0)
            {
                return "D";
            }
            else if (str.CompareTo("发") < 0)
            {
                return "E";
            }
            else if (str.CompareTo("旮") < 0)
            {
                return "F";
            }
            else if (str.CompareTo("铪") < 0)
            {
                return "G";
            }
            else if (str.CompareTo("讥") < 0)
            {
                return "H";
            }
            else if (str.CompareTo("咔") < 0)
            {
                return "J";
            }
            else if (str.CompareTo("垃") < 0)
            {
                return "K";
            }
            else if (str.CompareTo("嘸") < 0)
            {
                return "L";
            }
            else if (str.CompareTo("拏") < 0)
            {
                return "M";
            }
            else if (str.CompareTo("噢") < 0)
            {
                return "N";
            }
            else if (str.CompareTo("妑") < 0)
            {
                return "O";
            }
            else if (str.CompareTo("七") < 0)
            {
                return "P";
            }
            else if (str.CompareTo("亽") < 0)
            {
                return "Q";
            }
            else if (str.CompareTo("仨") < 0)
            {
                return "R";
            }
            else if (str.CompareTo("他") < 0)
            {
                return "S";
            }
            else if (str.CompareTo("哇") < 0)
            {
                return "T";
            }
            else if (str.CompareTo("夕") < 0)
            {
                return "W";
            }
            else if (str.CompareTo("丫") < 0)
            {
                return "X";
            }
            else if (str.CompareTo("帀") < 0)
            {
                return "Y";
            }
            else if (str.CompareTo("咗") < 0)
            {
                return "Z";
            }
            else
            {
                return "0";
            }
        }
        #endregion

        #region 基本属性
        /// <summary>
        /// 获取客户端IP地址
        /// 
        /// 创建: 邱柯皓 2009/02/04 19:47
        /// </summary>
        public static string UserIP
        {
            get
            {
                string str = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

                if (string.IsNullOrEmpty(str))
                    str = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

                return str;
            }
        }

        /// <summary>
        /// 获取当前URL地址
        /// 
        /// 创建: 邱柯皓 2009/03/11 23:54
        /// </summary>
        public static string CurrentUrl
        {
            get
            {
                //取本页URL地址 
                string strTemp = "";
                if (HttpContext.Current.Request.ServerVariables["HTTPS"] == "off")
                {
                    strTemp = "http://";
                }
                else
                {
                    strTemp = "https://";
                }

                strTemp = strTemp + HttpContext.Current.Request.ServerVariables["SERVER_NAME"];

                if (HttpContext.Current.Request.ServerVariables["SERVER_PORT"] != "80")
                {
                    strTemp = strTemp + ":" + HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
                }

                strTemp = strTemp + HttpContext.Current.Request.ServerVariables["URL"];

                if (HttpContext.Current.Request.QueryString != null)
                {
                    strTemp = strTemp + "?" + HttpContext.Current.Request.QueryString;
                }

                return strTemp;
            }
        }

        /// <summary>
        /// 获取当前物理路径
        /// 
        /// 创建: 邱柯皓 2009/02/04 19:49
        /// </summary>
        public static string CurrentPath
        {
            get
            {
                return HttpContext.Current.Server.MapPath("./");
            }
        }

        /// <summary>
        /// 获取站点根目录物理路径
        /// 
        /// 创建: 邱柯皓 2009/02/04 19:50
        /// </summary>
        public static string RootPath
        {
            get
            {
                return HttpContext.Current.Server.MapPath("~/");
            }
        }
        #endregion
    }
}